/*
 * ProtocolManager.java
 *
 * Created on 24 marzo 2007, 17.59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.bloomingstars.bluepad.server;

import com.bloomingstars.bluepad.comm.CommandCode;

/**
 *
 * @author vega
 */
public class ProtocolManager
{
    // 1X, 8X, 32X, 200X
    public static final double[] raSpeeds = { .000279, .002232, .008928, .0558 };
    public static final double[] decSpeeds = { .00418, .003344, .13376, .836 };
    
    /** Creates a new instance of ProtocolManager */
    public ProtocolManager()
    {
        // Start RA and Dec threads
        raInc.start();
        decInc.start();
    }
 
    public String getRAString()
    {
        String result;
        
        double ra1 = Math.abs(ra);
        
        int hours = (int) ra1;
        int mins = (int)((ra1 - (double) hours)*60.0);
        int secs = (int)((ra1 - hours - mins / 60.0) * 3600.0);
        result = "" + (int) ra + ":" + mins + ":" + secs + "#";        
        
        return result;
    }
    
    public String getDecString()
    {
        String result;
        
        double dec1 = Math.abs(dec);
        
        int degs = (int) dec1;
        int mins = (int)((dec1 - (double) degs)*60.0);
        int secs = (int)((dec1 - degs - mins / 60.0) * 3600.0);
        
        String ss = "";
        if (dec >-1.0 && dec < 0.0)
        {
            ss = "-";
        }
        result = "" + ss + (int) (degs*Math.signum(dec)) + "*" + mins + "'" + secs + "#";
        
        return result;
    }
    
    public String processCommand(int cmd)
    {
        String result = "";
        
        switch (cmd)
        {
            case CommandCode.SET_SPEED_1:
                raSpeed = raSpeeds[0];
                decSpeed = decSpeeds[0];
                break;
                
            case CommandCode.SET_SPEED_2:
                raSpeed = raSpeeds[1];
                decSpeed = decSpeeds[1];
                break;

            case CommandCode.SET_SPEED_3:
                raSpeed = raSpeeds[2];
                decSpeed = decSpeeds[2];
                break;

            case CommandCode.SET_SPEED_4:
                raSpeed = raSpeeds[3];
                decSpeed = decSpeeds[3];                
                break;
                
            case CommandCode.MOVE_EAST:
                raSign = 1.0;
                raInc.cont();
                synchronized (raInc.semaphore)
                {
                    raInc.semaphore.notify();
                }
                break;
                
            case CommandCode.MOVE_SOUTH:
                decSign = -1.0;
                decInc.cont();
                synchronized (decInc.semaphore)
                {
                    decInc.semaphore.notify();
                }                
                break;

            case CommandCode.MOVE_WEST:
                raSign = -1.0;
                raInc.cont();                
                synchronized (raInc.semaphore)
                {
                    raInc.semaphore.notify();
                }
                
                break;

            case CommandCode.MOVE_NORTH:
                decSign = 1.0;
                decInc.cont();
                synchronized (decInc.semaphore)
                {
                    decInc.semaphore.notify();
                }                
                break;

            case CommandCode.RELEASE_EAST:
                raInc.pause();
                break;
                
            case CommandCode.RELEASE_SOUTH:
                decInc.pause();
                break;

            case CommandCode.RELEASE_WEST:
                raInc.pause();
                break;

            case CommandCode.RELEASE_NORTH:
                decInc.pause();
                break;
                
            case CommandCode.GET_TELESCOPE_RA:
                result = getRAString();
                break;

            case CommandCode.GET_TELESCOPE_DEC:
                result = getDecString();
                break;                
        }
        
        return result;
    }
        
    
    class RAInc extends Thread
    {
        public void cont()
        {
            continueFlag = true;
        }
        
        public void pause()
        {
            continueFlag = false;
        }
        
        public void run()
        {
            while (true)
            {                
                synchronized (semaphore)
                {
                    try
                    {
                        semaphore.wait();
                    }
                    catch (Exception ex)
                    {
                        
                    }                    
                }
                
                while (continueFlag)
                {
                    try
                    {
                        Thread.sleep(250);
                        ra += raSign * raSpeed / 4.0;
                        if (ra < 0.0) 
                            ra += 24.0;
                        else if (ra > 24.0)
                            ra -= 24.0;
                        
                        System.out.println("RA :" + getRAString());
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
                
        private boolean continueFlag = true;
        public Object semaphore = new Object();
    }

    class DecInc extends Thread
    {   
        public void cont()
        {
            continueFlag = true;
        }
        
        public void pause()
        {
            continueFlag = false;
        }
        
        public void run()
        {
            while (true)
            {                
                synchronized (semaphore)
                {
                    try
                    {
                        semaphore.wait();
                    }
                    catch (Exception ex)
                    {
                        
                    }                    
                }
                
                while (continueFlag)
                {
                    try
                    {
                        Thread.sleep(250);
                        dec += decSign * decSpeed / 4.0;
                        
                        if (dec > 90.0)
                            dec -= 90.0;
                        else if (dec < -90.0)
                            dec += 90.0;
                        
                        System.out.println("Dec :" + getDecString());
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }
        
        private boolean continueFlag = true;
        
        public Object semaphore = new Object();
    }
    
    private double ra;
    private double dec;
    private double raSpeed;
    private double decSpeed;
    private double decSign = 1.0;
    private double raSign = 1.0;
    private RAInc raInc = new RAInc();
    private DecInc decInc = new DecInc();
}
